/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @author 品讯科技
 */

package com.px.pa.modulars.circle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.px.basic.alone.security.annotation.Inner;
import com.px.basic.alone.security.util.SecurityUtils;
import com.px.pa.modulars.circle.entity.CircleOrganizational;
import com.px.pa.modulars.circle.service.CircleOrganizationalService;
import com.px.pa.modulars.circle.vo.CircleOrganizationalListVo;
import com.px.pa.modulars.circle.vo.CircleOrganizationalVo;
import com.px.pa.modulars.upms.entity.SysUser;
import com.px.pa.modulars.upms.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author 品讯科技
 * @date 2024-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/circleorganizational" )
@Api(value = "circleorganizational", tags = "组织架构管理")
public class CircleOrganizationalController {

    private final  CircleOrganizationalService circleOrganizationalService;
    @Autowired
    private SysUserService sysUserService;
    private SysUser getUser(){
        SysUser user = sysUserService.getById(SecurityUtils.getUser().getId());
        return user;
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param circleOrganizational
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('circleOrganizational_circleorganizational_get')" )
    public R getCircleOrganizationalPage(Page page, CircleOrganizational circleOrganizational) {
        return R.ok(circleOrganizationalService.page(page, Wrappers.query(circleOrganizational)
                .orderByAsc("sort")));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('circleOrganizational_circleorganizational_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(circleOrganizationalService.getById(id));
    }

    /**
     * 新增
     * @param circleOrganizational
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    //@SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('circleOrganizational_circleorganizational_add')" )
    public R save(@RequestBody CircleOrganizational circleOrganizational) {
        circleOrganizational.setTenantCode(getUser().getOnlyLogo());
        return R.ok(circleOrganizationalService.save(circleOrganizational));
    }

    /**
     * 修改
     * @param circleOrganizational
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    //@SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('circleOrganizational_circleorganizational_edit')" )
    public R updateById(@RequestBody CircleOrganizational circleOrganizational) {
        return R.ok(circleOrganizationalService.updateById(circleOrganizational));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    //@SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('circleOrganizational_circleorganizational_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(circleOrganizationalService.removeById(id));
    }





    /**
     * 组织架构
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page/list" )
    public R getCircleOrganizationalPageList() {

      List<CircleOrganizational> circleOrganizationalVoList =  circleOrganizationalService.getList(getUser().getOnlyLogo());
        return R.ok(circleOrganizationalVoList);
    }


    /**
     * 组织架构
     * @return
     */
    @ApiOperation(value = "组织架构", notes = "组织架构")
    @GetMapping("/page/list/user" )
    public R getCircleOrganizationalPageListUser(@RequestParam(required = false) String key) {
        CircleOrganizationalListVo map =  circleOrganizationalService.getList1(key);
        return R.ok(map);
    }

    /**
     * 所属组织
     * @return
     */
    @ApiOperation(value = "所属组织", notes = "所属组织")
    @GetMapping("/list" )
    public R getCircleOrganizationalList() {
        String tenant = getUser().getOnlyLogo();
        List<CircleOrganizational> circleOrganizationalVoList =  circleOrganizationalService.list(new QueryWrapper<CircleOrganizational>()
                .eq("tenant_code",tenant)
        .ne("parent_id",0).orderByAsc("sort"));
        return R.ok(circleOrganizationalVoList);
    }


    /**
     * 组织架构
     * @return
     */
    @ApiOperation(value = "学习包指定学习人数", notes = "学习包指定学习人数")
    @GetMapping("/page/list/userBytask" )
    public R getCircleOrganizationalPageListUserBytask(@RequestParam("learnBack") String learnBack) {
        CircleOrganizationalListVo map =  circleOrganizationalService.getList2(learnBack);
        return R.ok(map);
    }



}
